package com.vision.util.http.down.thread;

import com.google.common.net.MediaType;
import com.vision.util.http.down.AbsDownEntity;
import com.vision.util.http.down.DownUtil;
import com.vision.util.http.exception.FileTypeException;
import com.vision.util.http.exception.HttpStatusException;
import com.vision.util.http.util.HttpRequestDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.Callable;

/**
 * 项目名称：com.zhongc
 * 类名称： DownThread
 * 类描述：
 * 创建人：zc
 * 创建时间：2017-01-04 23:51
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @version 1.0
 */
public class DownThread2<T extends AbsDownEntity> implements Callable<T> {

    private static final Logger logger = LoggerFactory.getLogger(DownThread2.class);


    /**
     * 二级目录地址
     */
    private String downPath;

    /**
     * 视频实体
     */
    private T downEntity;

    private HttpRequestDao httpRequestDao;

    private MediaType mediaType;

    /**
     * 下载线程
     *
     * @param downEntity     下载类
     * @param downPath
     * @param httpRequestDao
     */
    public DownThread2(T downEntity, String downPath, HttpRequestDao httpRequestDao, MediaType mediaType) {
        this.downEntity = downEntity;
        this.downPath = downPath;
        this.httpRequestDao = httpRequestDao;
        this.mediaType = mediaType;
    }

    @Override
    public T call() {
        try {
            if (httpRequestDao == null) {
                DownUtil.down(downPath, downEntity.getUrl(), downEntity.getFileName(), mediaType);
            } else {
                DownUtil.down(downPath, downEntity.getUrl(), downEntity.getFileName(), mediaType, httpRequestDao);
            }
        } catch (IOException | FileTypeException | HttpStatusException e) {
            logger.error("下载过程中出现异常:{}", e);
        }
        return downEntity;
    }
}
